﻿/*
 * Created By: Bharati On 04-Oct-10 @ 15:56
 * 
 */
using System;

namespace Jas
{
	
	public class ArrayManager<TElement>
	{
		Int32 capacity;
		Int32 index;
		Int32 mLastIndex;
		Int32 preAllocationDelta;
		TElement[] array;
		
		public ArrayManager(int capacity, int preAllocationDelta)
		{
			array=new TElement[capacity];
			index=0;
			mLastIndex=-1;
			this.capacity = array.Length;
			this.preAllocationDelta = preAllocationDelta;
		}
		
		public ArrayManager():this(100,50){}
		
		public void add(TElement element)
		{
			if(index>=capacity) resize();
			array[index]=element;
			mLastIndex=index++;
		}
		
		public int lastIndex {
			get { return mLastIndex; }
		}
		
		public Int32 currentIndex{
			get{
				return index;
			}
		}

		public TElement this[Int32 i] {
			get { 
				if(i<capacity)
					return array[i];
				else
					throw new IndexOutOfRangeException();
			}
			set { 
				index=i;
				add(value);
				}
				
		}
		
		
		
		
		protected void resize()
		{
			Array.Resize(ref array,capacity+preAllocationDelta);
			capacity=array.Length;
		}
	}
}
